<template>
  <div class="app-main-container">
    <div class="app-main-title">
      常见问题
      <div class="goback" @click="$router.replace(`/options/noviceHelpList`)">
        <i class="el-icon-refresh-left" /> 返回上一页
      </div>
    </div>
    <el-form
      ref="ruleForm"
      class="doumee-form"
      :model="param"
      :rules="rules"
      label-width="100px"
      label-suffix="："
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="param.title"
          class="w400"
          type="text"
          placeholder="请输入标题"
          maxlength="20"
        />
      </el-form-item>
      <el-form-item label="内容" prop="info">
        <el-input
          v-model="param.info"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 10}"
          placeholder="请输入标题"
          maxlength="500"
        />
      </el-form-item>
      <el-form-item label="适用场馆" prop="venueIds">
        <el-checkbox-group v-model="param.venueIds">
          <el-checkbox v-for="(item, index) in cgList" :key="index" :label="item.id">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="排序码" prop="sort">
        <el-input
          v-model="param.sort"
          class="w400"
          type="number"
          placeholder="请输入排序码"
          maxlength="20"
        />
      </el-form-item>
      <!-- <div class="line-style" /> -->
      <el-form-item>
        <el-button
          v-preventReClick
          :loading="subLoading"
          type="primary"
          @click="onSubmit()"
        >提交</el-button>
      </el-form-item></el-form>
  </div>
</template>

<script>
import { addKnowledge, editKnowledge, infoWXKnowledge, venueList } from '@/api'
export default {
  name: 'NoviceHelpEdit',
  data() {
    return {
      subLoading: false,
      param: {
        venueIds: []
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        info: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        sort: [
          { pattern: /^\+?[1-9][0-9]*$/, required: true, message: '请输入排序码', trigger: 'blur' }
        ],
        venueIds: [
          { required: true, message: '请选择适用场馆', trigger: 'change' }
        ]
      },
      edit: false,
      cgList: [],
      id: ''
    }
  },
  mounted() {
    this.getVenueList()
    if (this.$route.query.id) {
      this.$route.meta.title = '编辑常见问题'
      this.id = this.$route.query.id
      this.edit = true
      this.getData()
    } else {
      this.$route.meta.title = '新增常见问题'
    }
  },
  methods: {
    getData() {
      infoWXKnowledge({
        param: { id: this.id }
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.param = res.record
        })
    },
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: {}
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.cgList = res.recordList
        })
    },
    onSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const param = { ...this.param }
          param.type = '2'
          this.subLoading = true
          if (this.edit) {
            editKnowledge({
              param
            }).then(res => {
              this.subLoading = false
              if (res.errorCode === '000000') {
                this.$message.success('提交成功')
                this.$router.replace(`/options/noviceHelpList?renovate=1`)
              }
            }).catch(err => {
              this.subLoading = false
              console.log(err)
            })
          } else {
            param.status = '1'
            addKnowledge({
              param
            }).then(res => {
              this.subLoading = false
              if (res.errorCode === '000000') {
                this.$message.success('提交成功')
                this.$router.replace(`/options/noviceHelpList?renovate=1`)
              }
            }).catch(err => {
              this.subLoading = false
              console.log(err)
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";

.device-uploader {
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  cursor: pointer;
  border: 1px dashed #ccc;
  border-radius: 8px;
  transition: all 0.3s;
  margin-right: 20px;
  &:hover {
    border-color: $menuActive;
    .uploader-icon {
      color: $menuActive;
    }
  }
  ::v-deep .el-upload {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    .file {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      background-size: contain;
      background-position: center;
      background-repeat: no-repeat;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      .delete {
        background-color: rgba(0, 0, 0, 0.5);
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        display: none;
        align-items: center;
        justify-content: center;
        .icon {
          color: #fff;
        }
      }
      &:hover {
        .delete {
          display: flex;
        }
      }
    }
  }
  .uploader-icon {
    color: #aaaaaa;
    font-size: 22px;
    transition: all 0.3s;
  }
}
.line-style {
  margin-bottom: 20px;
}
.device-uploaders {
  ::v-deep .el-upload-list__item {
    width: 80px;
    height: 80px;
  }
  ::v-deep .el-upload {
    width: 80px;
    height: 80px;
    line-height: 80px;
  }
}
</style>
